package simulation;

/**
 * 外观数列
 */
public class Demo20250531 {
    public String countAndSay(int n) {
        // 起始是 "1"
        String ret = "1";
        // 解释 n - 1 次 ret
        for (int i = 1; i < n; i++) {
            StringBuilder tmp = new StringBuilder();
            int len = ret.length();
            for (int left = 0, right = 0; right < len; ) {
                while (right < len && ret.charAt(left) == ret.charAt(right)) right++;
                tmp.append(Integer.toString(right - left));
                tmp.append(ret.charAt(left));
                left = right;
            }
            ret = tmp.toString();
        }
        return ret;
    }
}
